package me.shurufa.activities;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.fragments.UserInfoCommentFragment;
import me.shurufa.fragments.UserInfoMarkFragment;
import me.shurufa.model.User;
import me.shurufa.utils.Constants;
import me.shurufa.widget.CircleImageView;

/**
 * 其他用户信息界面
 *
 * @FileName: me.shurufa.activities.UserInfoActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-18 16:16
 */
public class UserInfoActivity extends BaseActivity {

    @Bind(R.id.right_image)
    ImageView rightImage;
    @Bind(R.id.user_avatar)
    CircleImageView userAvatar;
    @Bind(R.id.user_nick)
    TextView userNick;
    @Bind(R.id.user_desc)
    TextView userDesc;
    @Bind(R.id.user_comment_container)
    FrameLayout userCommentContainer;
    @Bind(R.id.user_mark_container)
    FrameLayout userMarkContainer;
    @Bind(R.id.toolbar_divider)
    View toolbarDivider;
    @Bind(R.id.user_points)
    TextView userPoints;
    @Bind(R.id.user_digests)
    TextView userDigests;


    private User mUser;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_user_info;
        super.onCreate(savedInstanceState);
        mUser = new Gson()
                .fromJson(getIntent().getStringExtra(Constants.EXTRA_USER_PARAM), User.class);
        if (mUser == null)
            return;
        uiInit();
    }

    private void uiInit() {
        rightImage.setImageResource(R.drawable.ic_talk);
        rightImage.setVisibility(View.GONE);
        toolbarDivider.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(mUser.avatar)) {
            Glide.with(this).load(mUser.avatar).into(userAvatar);
        } else {
            userAvatar.setImageResource(R.drawable.default_avatar);
        }
        if (!TextUtils.isEmpty(mUser.nickname)) {
            userNick.setText(mUser.nickname);
        }
        if (!TextUtils.isEmpty(mUser.description)) {
            userDesc.setText(mUser.description);
        }
        userPoints.setText(TextUtils.concat(String.valueOf(mUser.credits_amount),getString(R.string.points)), TextView.BufferType.SPANNABLE);
        userDigests.setText(TextUtils.concat(String.valueOf(mUser.excerpt_total),getString(R.string.item)), TextView.BufferType.SPANNABLE);

        initFragment();
    }

    private void initFragment() {
        UserInfoCommentFragment userInfoCommentFragment = UserInfoCommentFragment.newInstance(mUser);
        UserInfoMarkFragment userInfoMarkFragment = UserInfoMarkFragment.newInstance(mUser);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.user_comment_container, userInfoCommentFragment);
        transaction.add(R.id.user_mark_container, userInfoMarkFragment);
        transaction.commit();
    }
}
